import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xuance/bean/GlobalType.dart';
import 'package:xuance/bean/ReceiveDatabean.dart';
import 'package:xuance/bean/TimeEnum.dart';
import 'package:xuance/bean/global_event_bean.dart';
import 'package:xuance/constant.dart';
import 'package:xuance/generated/assets.dart';
import 'package:xuance/main.dart';

class FloatingPanelService extends GetxController {
  var min_status = true.obs;
  RxDouble overlayWidth = (100.0).obs;
  RxDouble overlayheight = (50.0).obs;
  Rx<Offset> offset = const Offset(300, 200).obs;
  final OverlayEntry _overlayEntry = OverlayEntry(builder: (context) {
    return GetBuilder(
      assignId: true,
      init: FloatingPanelService(),
      builder: (logic) {
        return Obx(() {
          double dx = 0.0;
          double dy = 0.0;

          if ((logic.offset.value.dx > 0) &&
              ((logic.offset.value.dx + logic.overlayWidth.value) <
                  Get.width)) {
            dx = logic.offset.value.dx;
          } else if ((logic.offset.value.dx + logic.overlayWidth.value) >=
              Get.width) {
            dx = Get.width - logic.overlayWidth.value;
          } else {
            dx = 0;
          }
          if ((logic.offset.value.dy > 0) &&
              ((logic.offset.value.dy + logic.overlayWidth.value) <
                  Get.height)) {
            dy = logic.offset.value.dy;
          } else if ((logic.offset.value.dy + logic.overlayWidth.value) >=
              Get.height) {
            dy = Get.width - logic.overlayWidth.value;
          } else {
            dy = 0;
          }
          return Positioned(
            top: dy,
            left: dx,
            child: Draggable(
              feedback: _contentBody(isFeedback: true),
              child: _contentBody(isFeedback: false),
              onDraggableCanceled: (Velocity velocity, Offset offset) {
                logic.offset.value = offset;
              },
              onDragStarted: () {
                logic.draging.value = true;
              },
              onDragEnd: (a) {
                logic.draging.value = false;
              },
            ),
          );
        });
      },
    );
  });

  var draging = false.obs;

  var dateType = TimeEnum.month.obs;

  var hasFilter = false.obs;
  var controller = TextEditingController();

  @override
  void onInit() {
    super.onInit();
    WidgetsBinding.instance.addPostFrameCallback((callback) {
      Overlay.of(Get.context!).insert(_overlayEntry);
    });
  }

  @override
  void onClose() {
    super.onClose();
    _overlayEntry.remove();
  }

  static _contentBody({required bool isFeedback}) {
    final logic = Get.find<FloatingPanelService>();
    return Obx(() {
      if (logic.draging.value && !isFeedback) {
        return Container();
      }
      return GestureDetector(
        onTap: () {
          if (logic.overlayWidth.value == 100) {
            logic.min();
          } else {
            logic.hide();
          }
        },
        child: AnimatedContainer(
            padding: const EdgeInsets.symmetric(vertical: 5),
            width: logic.overlayWidth.value,
            height: logic.overlayheight.value,
            decoration: BoxDecoration(
                color: bgColor,
                borderRadius: br5,
                border: Border.all(color: c_green, width: 2)),
            duration: const Duration(milliseconds: 200),
            child: Obx(() {
              if (logic.overlayWidth.value == 100) {
                return Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      Row(children: [
                        Image.asset(Assets.assetsTimeIcon,
                            scale: 2.5, color: c_green),
                        const SizedBox(width: 5),
                        Obx(() {
                          return Text(logic.dateType.value.desc,
                              style: TextStyle(
                                  color: c_green, fontWeight: FontWeight.bold));
                        })
                      ]),
                      const SizedBox(width: 5),
                      Obx(() {
                        return Icon(Icons.filter_list_off,
                            color:
                                logic.hasFilter.value ? Colors.white : c_green);
                      })
                    ]);
              } else {
                return SingleChildScrollView(
                    child: Column(children: [
                  Padding(
                    padding: const EdgeInsets.symmetric(vertical: 10),
                    child: SingleChildScrollView(
                      scrollDirection: Axis.horizontal,
                      child: Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: [
                            Row(children: [
                              Image.asset(Assets.assetsTimeIcon,
                                  scale: 2.5, color: c_green),
                              const SizedBox(width: 5),
                              Obx(() {
                                return Text(logic.dateType.value.desc,
                                    style: TextStyle(
                                        fontWeight: FontWeight.bold,
                                        color: c_green));
                              })
                            ]),
                            const SizedBox(width: 5),
                            Obx(() {
                              return Icon(Icons.filter_list_off,
                                  color: logic.hasFilter.value
                                      ? Colors.white
                                      : c_green);
                            }),
                            const SizedBox(width: 15)
                          ]),
                    ),
                  ),
                  Container(
                      margin: const EdgeInsets.symmetric(horizontal: 5),
                      child: SingleChildScrollView(
                          child: Column(children: [
                        Padding(
                            padding: const EdgeInsets.symmetric(horizontal: 10),
                            child: SingleChildScrollView(
                                scrollDirection: Axis.horizontal,
                                child: Row(children: [
                                  SizedBox(
                                      width: Get.width - 40,
                                      child: IntrinsicHeight(
                                          child: Row(children: [
                                        Expanded(
                                            child: Container(
                                                decoration: BoxDecoration(
                                                    color: Colors.white,
                                                    borderRadius: br5),
                                                child: TextField(
                                                    controller:
                                                        logic.controller,
                                                    cursorColor: c_green,
                                                    decoration:
                                                        const InputDecoration(
                                                            isCollapsed: true,
                                                            isDense: true,
                                                            border: InputBorder
                                                                .none,
                                                            contentPadding:
                                                                EdgeInsets
                                                                    .symmetric(
                                                                        horizontal:
                                                                            10,
                                                                        vertical:
                                                                            8),
                                                            hintText:
                                                                "请输入搜索内容")))),
                                        const SizedBox(width: 10),
                                        GestureDetector(
                                            onTap: () {
                                              logic.hasFilter.value = logic
                                                  .controller.text.isNotEmpty;
                                              logic.hide();
                                              eventBus.fire(GlobalEventBean(
                                                  data: ReceiveDataBean(
                                                      time:
                                                          logic.dateType.value,
                                                      txt: logic
                                                          .controller.text),
                                                  type: GlobalType.updateDate));
                                            },
                                            child: Container(
                                                padding:
                                                    const EdgeInsets.symmetric(
                                                        horizontal: 25),
                                                decoration: BoxDecoration(
                                                    border: Border.all(
                                                        color: c_green),
                                                    borderRadius: br5),
                                                child: Center(
                                                    child: Text("搜索",
                                                        style: TextStyle(
                                                            color: c_green)))))
                                      ])))
                                ]))),
                        const SizedBox(height: 20),
                        Padding(
                          padding: const EdgeInsets.symmetric(horizontal: 10),
                          child: Row(
                              children: TimeEnum.values
                                  .map((m) => Expanded(
                                        child: GestureDetector(
                                          onTap: () {
                                            logic.dateType.value = m;
                                          },
                                          child: Obx(() {
                                            return Container(
                                              margin:
                                                  const EdgeInsets.symmetric(
                                                      horizontal: 3),
                                              padding:
                                                  const EdgeInsets.symmetric(
                                                      horizontal: 15,
                                                      vertical: 5),
                                              decoration: BoxDecoration(
                                                  borderRadius: br5,
                                                  color:
                                                      logic.dateType.value == m
                                                          ? c_green
                                                          : null,
                                                  border: Border.all(
                                                      color: c_green)),
                                              child: Text(
                                                m.desc,
                                                style: TextStyle(
                                                    color:
                                                        logic.dateType.value ==
                                                                m
                                                            ? Colors.white
                                                            : c_green,
                                                    fontWeight:
                                                        FontWeight.bold),
                                              ),
                                            );
                                          }),
                                        ),
                                      ))
                                  .toList()),
                        )
                      ])))
                ]));
              }
            })),
      );
    });
  }

  void hide() {
    overlayWidth.value = 100;
    overlayheight.value = 50;
  }

  void min() {
    overlayWidth.value = Get.width;
    overlayheight.value = 180;
  }
}
